package com.ruoyi.project.blog.service.impl;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.blog.domain.vo.*;
import com.ruoyi.project.blog.mapper.*;
import com.ruoyi.project.blog.service.IBIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BIndexServiceImpl implements IBIndexService {
    @Autowired
    private BArticleMapper bArticleMapper;
    @Autowired
    private BCategoryMapper categoryMapper;
    @Autowired
    private BTagsMapper tagsMapper;
    @Autowired
    private BFriendLinkMapper friendLinkMapper;

    @Override
    public AjaxResult getIndexStatistics(String userId) {
        //文章数量
        int articleTotal = bArticleMapper.getIndexStatistics(userId);
        //标签数量
        int tagsTotal = tagsMapper.getIndexStatistics(userId);
        //分类数量
        int categoryTotal = categoryMapper.getIndexStatistics(userId);
        //友情链接数量
        int linksTotal = friendLinkMapper.getIndexStatistics(userId);
        Map<String,Object> result = new HashMap<>();
        result.put("articleTotal",articleTotal);
        result.put("tagsTotal",tagsTotal);
        result.put("categoryTotal",categoryTotal);
        result.put("linksTotal",linksTotal);
        return AjaxResult.success(result);
    }

    @Override
    public Map<String, Object> initCategory(String userId) {
        //分类统计
        Map<String, Object> categoryCount = this.categoryCount(userId);
        return categoryCount;
    }
    public Map<String, Object> categoryCount(String userId){
        Map<String, Object> map = new HashMap<>();
        List<SystemCategoryCountVO> result = categoryMapper.countArticle(userId);
        List<String> list = new ArrayList<>();
        result.forEach(item -> list.add(item.getName()));
        List<Map<String,Object>> tagsList = tagsMapper.countTags(userId);
        List tagsList1 = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        for (int i=0;i<tagsList.size();i++){
            List objects = new ArrayList<>();
            objects.add(tagsList.get(i).get("name").toString());
            objects.add(tagsList.get(i).get("id"));
            tagsList1.add(objects);
        }


        map.put("result",result);
        map.put("categoryList",list);
        map.put("tagsList",tagsList1);
        return map;
    }
}
